package com.herui.ota.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.herui.ota.config.ControllerNames;
import com.herui.ota.utils.AddressUtils;
import com.mlnx.common.entity.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mapstruct.Context;
import org.shan.spring.base.BaseController;
import org.shan.spring.base.annotations.SysLog;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(value = ControllerNames.EchoName + "接口", tags = {ControllerNames.EchoName + "接口"})
@SysLog(title = ControllerNames.EchoName)
@RestController
@RequestMapping("/echo")
@Slf4j
public class EchoController extends BaseController {

    @ApiOperation(value = "echo", notes = "")
    @GetMapping({"/echo"})
    public ResponseData<String> echo(String msg, HttpServletRequest request) {
        log.info("echo:{}", msg);
        String ipAddr = AddressUtils.getIpAddr(request);
        return this.result("hello " + msg+"   ipAddr:"+ipAddr);
    }

    @PostMapping("/postGateway")
    public JSONObject postGateway(@RequestBody JSONObject data, @Context HttpServletRequest request,
                                  @Context HttpServletResponse response) throws IOException {
        log.info("请求的json：{}", data.toString());
        //		getAllRequests(request);
        // 接收响应的数据
        JSONObject responseJson = null;
        // 获取请求头的授权token
        // String authorization = getRequestHeaderByAuthorization(request);
        // authorization = StringUtils.remove(authorization,"Bearer ");
        log.info("data开始");
        String requestId = data.getString("requestId");
        log.info("data结束" + requestId);
        try {
            // 获取请求的数据
            JSONArray inputs = data.getJSONArray("inputs");
            JSONObject intent = (JSONObject) inputs.get(0);
            String namespace = (String) intent.get("intent");
            String resData = null;
            switch (namespace) {
                case "action.devices.SYNC":
                    log.info("同步设备请求----action.devices.SYNC");
                    responseJson = parseSYNC();
                    responseJson.put("requestId", requestId);
                    break;
                case "action.devices.QUERY":
                    log.info("查询设备请求----action.devices.QUERY");
                    responseJson = parseQuery();
                    responseJson.put("requestId", requestId);
                    // resData = googleIotQuery.query(data,authorization);
                    // log.info("查询设备返回的信息为："+resData);
                    // responseJson = new JSONObject(resData);
                    break;
                case "action.devices.EXECUTE":
                    log.info("执行设备请求----action.devices.EXECUTE");
                    // resData = googleIotController.controller(data,authorization);
                    // log.info("执行请求返回的信息为："+resData);
                    // responseJson = new JSONObject(resData);
                    break;
                case "action.devices.DISCONNECT":
                    log.info("取消设备连接请求----action.devices.DISCONNECT");
                    responseJson = new JSONObject();
                    break;
                default:

                    // String errorCode = GoogleConstants.FUNCTION_NOT_SUPPORTED;
                    // JSONObject errorResponse = creatErrorResponse(requestId,errorCode);
                    // responseJson = new JSONObject(errorResponse.toString());
                    break;
            }
        } catch (Exception e) {
            log.error(e.toString());
            e.printStackTrace();
            // String errorCode = GoogleConstants.HARD_ERROR; //很抱歉，出现了错误，我无法控制您的家用设备。
            // JSONObject errorResponse = creatErrorResponse(requestId,errorCode);
            // responseJson = new JSONObject(errorResponse.toString());
        }
        log.info("返回数据：" + responseJson.toJSONString());
        return responseJson;

    }

    private JSONObject parseSYNC() {
        String str =
                "{\n" + "\t\"requestId\": \"6894439706274654512\",\n" + "\t\"payload\": {\n" + "\t\t\"agentUserId\": \"user123\",\n" + "\t\t" +
                "\"devices\": [{\n" + "\t\t\t\"id\": \"123\",\n" + "\t\t\t\"type\": \"action.devices.types.AIRPURIFIER\",\n" + "\t\t\t\"traits" +
                "\": [\n" + "\t\t\t\t\"action.devices.traits.OnOff\",\n" + "\t\t\t\t\"action.devices.traits.LockUnlock\",\n" + "\t\t\t\t" +
                "\"action.devices.traits.Modes\",\n" + "\t\t\t\t\"action.devices.traits.Brightness\"\n" + "\t\t\t],\n" + "\t\t\t\"name\": {\n" +
                "\t\t\t\t\"name\": \"Air purifier\"\n" + "\t\t\t},\n" + "\t\t\t\"willReportState\": true,\n" + "\t\t\t\"attributes\": {\n" + "\t\t" + "\t\t\"availableModes\": [{\n" + "\t\t\t\t\t\"name\": \"mode\",\n" + "\t\t\t\t\t\"name_values\": [{\n" + "\t\t\t\t\t\t" + "\"name_synonym\": [\n" + "\t\t\t\t\t\t\t\"Mode\"\n" + "\t\t\t\t\t\t],\n" + "\t\t\t\t\t\t\"lang\": \"en\"\n" + "\t\t\t\t\t}],\n" + "\t\t\t\t\t\"settings\": [{\n" + "\t\t\t\t\t\t\t\"setting_name\": \"auto\",\n" + "\t\t\t\t\t\t\t\"setting_values\": [{\n" + "\t\t\t" + "\t\t\t\t\t\"setting_synonym\": [\n" + "\t\t\t\t\t\t\t\t\t\"Auto\"\n" + "\t\t\t\t\t\t\t\t],\n" + "\t\t\t\t\t\t\t\t\"lang\": " + "\"en\"\n" + "\t\t\t\t\t\t\t}]\n" + "\t\t\t\t\t\t},\n" + "\t\t\t\t\t\t{\n" + "\t\t\t\t\t\t\t\"setting_name\": \"sleep\",\n" + "\t\t" + "\t\t\t\t\t\"setting_values\": [{\n" + "\t\t\t\t\t\t\t\t\"setting_synonym\": [\n" + "\t\t\t\t\t\t\t\t\t\"Sleep\"\n" + "\t\t\t\t\t\t" + "\t\t],\n" + "\t\t\t\t\t\t\t\t\"lang\": \"en\"\n" + "\t\t\t\t\t\t\t}]\n" + "\t\t\t\t\t\t},\n" + "\t\t\t\t\t\t{\n" + "\t\t\t\t\t\t\t" + "\"setting_name\": \"1_F\",\n" + "\t\t\t\t\t\t\t\"setting_values\": [{\n" + "\t\t\t\t\t\t\t\t\"setting_synonym\": [\n" + "\t\t\t\t" + "\t\t\t\t\t\"One\",\n" + "\t\t\t\t\t\t\t\t\t\"Small\"\n" + "\t\t\t\t\t\t\t\t],\n" + "\t\t\t\t\t\t\t\t\"lang\": \"en\"\n" + "\t\t\t" + "\t\t\t\t}]\n" + "\t\t\t\t\t\t},\n" + "\t\t\t\t\t\t{\n" + "\t\t\t\t\t\t\t\"setting_name\": \"2_F\",\n" + "\t\t\t\t\t\t\t" + "\"setting_values\": [{\n" + "\t\t\t\t\t\t\t\t\"setting_synonym\": [\n" + "\t\t\t\t\t\t\t\t\t\"Two\",\n" + "\t\t\t\t\t\t\t\t\t" + "\"Medium\"\n" + "\t\t\t\t\t\t\t\t],\n" + "\t\t\t\t\t\t\t\t\"lang\": \"en\"\n" + "\t\t\t\t\t\t\t}]\n" + "\t\t\t\t\t\t},\n" + "\t\t" + "\t\t\t\t{\n" + "\t\t\t\t\t\t\t\"setting_name\": \"3_F\",\n" + "\t\t\t\t\t\t\t\"setting_values\": [{\n" + "\t\t\t\t\t\t\t\t" + "\"setting_synonym\": [\n" + "\t\t\t\t\t\t\t\t\t\"Three\",\n" + "\t\t\t\t\t\t\t\t\t\"Large\"\n" + "\t\t\t\t\t\t\t\t],\n" + "\t\t\t" + "\t\t\t\t\t\"lang\": \"en\"\n" + "\t\t\t\t\t\t\t}]\n" + "\t\t\t\t\t\t}\n" + "\t\t\t\t\t],\n" + "\t\t\t\t\t\"ordered\": true\n" + "\t\t\t\t}]\n" + "\t\t\t},\n" + "\t\t\t\"deviceInfo\": {\n" + "\t\t\t\t\"manufacturer\": \"smart-home-inc\",\n" + "\t\t\t\t\"model" + "\": \"hs1234\",\n" + "\t\t\t\t\"hwVersion\": \"3.2\",\n" + "\t\t\t\t\"swVersion\": \"11.4\"\n" + "\t\t\t}\n" + "\t\t},\n" + "\t\t" + "{\n" + "\t\t\t\"id\": \"113\",\n" + "\t\t\t\"type\": \"action.devices.types.AIRPURIFIER\",\n" + "\t\t\t\"traits\": [\n" + "\t\t\t" + "\t\"action.devices.traits.OnOff\",\n" + "\t\t\t\t\"action.devices.traits.LockUnlock\",\n" + "\t\t\t\t\"action.devices.traits" + ".Modes\",\n" + "\t\t\t\t\"action.devices.traits.Brightness\"\n" + "\t\t\t],\n" + "\t\t\t\"name\": {\n" + "\t\t\t\t\"name\": \"Air " + "purifier\"\n" + "\t\t\t},\n" + "\t\t\t\"willReportState\": true,\n" + "\t\t\t\"attributes\": {\n" + "\t\t\t\t\"availableModes\": " + "[{\n" + "\t\t\t\t\t\"name\": \"mode\",\n" + "\t\t\t\t\t\"name_values\": [{\n" + "\t\t\t\t\t\t\"name_synonym\": [\n" + "\t\t\t\t\t" + "\t\t\"Mode\"\n" + "\t\t\t\t\t\t],\n" + "\t\t\t\t\t\t\"lang\": \"en\"\n" + "\t\t\t\t\t}],\n" + "\t\t\t\t\t\"settings\": [{\n" + "\t" + "\t\t\t\t\t\t\"setting_name\": \"auto\",\n" + "\t\t\t\t\t\t\t\"setting_values\": [{\n" + "\t\t\t\t\t\t\t\t\"setting_synonym\": [\n" + "\t\t\t\t\t\t\t\t\t\"Auto\"\n" + "\t\t\t\t\t\t\t\t],\n" + "\t\t\t\t\t\t\t\t\"lang\": \"en\"\n" + "\t\t\t\t\t\t\t}]\n" + "\t\t\t\t\t\t},\n" + "\t\t\t\t\t\t{\n" + "\t\t\t\t\t\t\t\"setting_name\": \"sleep\",\n" + "\t\t\t\t\t\t\t\"setting_values\": [{\n" + "\t\t\t\t\t\t\t\t\"setting_synonym\": [\n" + "\t\t\t\t\t\t\t\t\t\"Sleep\"\n" + "\t\t\t\t\t\t\t\t],\n" + "\t\t\t\t\t\t\t\t\"lang\": \"en\"\n" + "\t\t\t\t\t\t\t}]\n" + "\t\t\t\t\t\t},\n" + "\t\t\t\t\t\t{\n" + "\t\t\t\t\t\t\t\"setting_name\": \"1_F\",\n" + "\t\t\t\t\t\t\t\"setting_values\": [{\n" + "\t\t\t\t\t\t\t\t\"setting_synonym\": [\n" + "\t\t\t\t\t\t\t\t\t\"One\",\n" + "\t\t\t\t\t\t\t\t\t\"Small\"\n" + "\t\t\t\t\t\t\t\t],\n" + "\t\t\t\t\t\t\t\t\"lang\": \"en\"\n" + "\t\t\t\t\t\t\t}]\n" + "\t\t\t\t\t\t},\n" + "\t\t\t\t\t\t{\n" + "\t\t\t\t\t\t\t\"setting_name\": \"2_F\",\n" + "\t\t\t\t\t\t\t\"setting_values\": [{\n" + "\t\t\t\t\t\t\t\t\"setting_synonym\": [\n" + "\t\t\t\t\t\t\t\t\t\"Two\",\n" + "\t\t\t\t\t\t\t\t\t\"Medium\"\n" + "\t\t\t\t\t\t\t\t],\n" + "\t\t\t\t\t\t\t\t\"lang\": \"en\"\n" + "\t\t\t\t\t\t\t}]\n" + "\t\t\t\t\t\t},\n" + "\t\t\t\t\t\t{\n" + "\t\t\t\t\t\t\t\"setting_name\": \"3_F\",\n" + "\t\t\t\t\t\t\t\"setting_values\": [{\n" + "\t\t\t\t\t\t\t\t\"setting_synonym\": [\n" + "\t\t\t\t\t\t\t\t\t\"Three\",\n" + "\t\t\t\t\t\t\t\t\t\"Large\"\n" + "\t\t\t\t\t\t\t\t],\n" + "\t\t\t\t\t\t\t\t\"lang\": \"en\"\n" + "\t\t\t\t\t\t\t}]\n" + "\t\t\t\t\t\t}\n" + "\t\t\t\t\t],\n" + "\t\t\t\t\t\"ordered\": true\n" + "\t\t\t\t}]\n" + "\t\t\t},\n" + "\t\t\t\"deviceInfo\": {\n" + "\t\t\t\t\"manufacturer\": \"smart-home-inc\",\n" + "\t\t\t\t\"model\": \"hs1234\",\n" + "\t\t\t\t\"hwVersion\": \"3.2\",\n" + "\t\t\t\t\"swVersion\": \"11.4\"\n" + "\t\t\t}\n" + "\t\t}]\n" + "\t}\n" + "}";
        return JSON.parseObject(str);
    }

    private JSONObject parseQuery() {
        String str =
                "{\n" + "\t\"requestId\": \"6894439706274654514\",\n" + "\t\"payload\": {\n" + "\t\t\"devices\": {\n" + "\t\t\t\"123\": {\n" + "\t" + "\t\t\t\"status\": \"SUCCESS\",\n" + "\t\t\t\t\"online\": true,\n" + "\t\t\t\t\"on\": true,\n" + "\t\t\t\t\"lock\": false,\n" + "\t" + "\t\t\t\"brightness\": 0,\n" + "\t\t\t\t\"currentModeSettings\": {\n" + "\t\t\t\t\t\"mode\": \"auto\"\n" + "\t\t\t\t}\n" + "\t\t\t" + "},\n" + "\t\t\t\"113\": {\n" + "\t\t\t\t\"status\": \"SUCCESS\",\n" + "\t\t\t\t\"online\": true,\n" + "\t\t\t\t\"on\": true,\n" + "\t\t\t\t\"lock\": false,\n" + "\t\t\t\t\"brightness\": 0,\n" + "\t\t\t\t\"currentModeSettings\": {\n" + "\t\t\t\t\t\"mode\": " + "\"auto\"\n" + "\t\t\t\t}\n" + "\t\t\t}\n" + "\t\t}\n" + "\t}\n" + "}";
        return JSON.parseObject(str);
    }
}
